package com.tronapi.c.service;

import com.tronapi.c.dao.TokenInfoDao;
import com.tronapi.c.model.request.SendTokenRequest;
import com.tronapi.c.model.d.TokenInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class TokenService {

    @Autowired
    private TokenInfoDao tokenInfoDao;

    /**
     * auto 发送token指令
     */
    public void sendToken(SendTokenRequest request) {
        TokenInfo tokenInfo = new TokenInfo();
        tokenInfo.setDeviceId(request.getDeviceId());
        tokenInfo.setStatus(1);
        tokenInfo.setVal(request.getVal());
        tokenInfoDao.insert(tokenInfo);
    }

    /**
     * app 获取token
     *
     * @param deviceId
     * @return
     */
    public List<TokenInfo> findTokenToClient(String deviceId, String appName) {
        List<TokenInfo> tokensList = tokenInfoDao.findByStatus(deviceId, appName, 1);
        for (TokenInfo token : tokensList) {
            token.setStatus(0);
            token.setUseTime(new Date());
            tokenInfoDao.updateById(token);
        }
        return tokensList;
    }
}
